package com.asjy.controller;

import com.asjy.po.CategoryExt;
import com.asjy.po.Product;
import com.asjy.service.CategoryService;
import com.asjy.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.List;
import java.util.Map;

/**
 * @Auther: lilinlin
 * @Date: 2021/4/9
 * @Description: com.asjy.controller
 * @version: 1.0
 */
@Controller
public class IndexController {
    @Resource
    private CategoryService categoryService;

    @Resource
    private ProductService productService;

    @Resource
    private ServletContext application;

    @RequestMapping("/index")
    public String toIndex(Model model){
        List<CategoryExt> indexNav = (List<CategoryExt>)application.getAttribute("indexNav");
        //通过indexNav，找到二级类目，再通过二级类目id遍历商品
        //key:二级类目id，value：当前二级类目下所有商品的集合
        Map<Integer,List<Product>> productMap= productService.getProduct(indexNav);
        model.addAttribute("indexNav",indexNav);
        model.addAttribute("productMap",productMap);
        return "index";
    }
}